#include "ui_bofugui.h"
#include "bofugui.h"

void BofuGui::on_pushButton_DeleteLog_clicked()
{
    command = "rm "+dir.absolutePath()+"/"+korpusname+".log";
    commandexec = system(command.toAscii().data());
    ui->logText->clear();
}

void BofuGui::on_pushButton_openLog_clicked()
{
    openURL(QUrl( ""+dir.absolutePath()+"/"+korpusname+".log" ));
}

void BofuGui::logDate()
{
    writeToLog("echo \"-------------------------------------------\" >> "+dir.absolutePath()+"/"+korpusname+".log");
    writeToLog("date '+DATE: %m/%d/%y TIME:%H:%M:%S' >> "+dir.absolutePath()+"/"+korpusname+".log");
    writeToLog("echo \"-------------------------------------------\" >> "+dir.absolutePath()+"/"+korpusname+".log");
}

void BofuGui::check_existing_files()
{
    //TODO: update or check if usefull at all
    QDir::setSearchPaths("activeProject", QStringList(dir.absolutePath()));
    transcriptionfile.setFileName("activeProject:transcription.txt");
    convert_tables.setFileName("activeProject:convert_tables.txt");
    filelist.setFileName("activeProject:filelist.txt");
    dict.setFileName("activeProject:proto/dict");
    unitlist.setFileName("activeProject:unitlist.txt");
//    QString list(settings.value("filelist").toString());
//    filelist.setFileName("activeProject:"+list);
    convert_tables.setFileName("activeProject:convert_tables.txt");
    contextfile.setFileName("activeProject:context_classes.txt");
    boss3conf.setFileName("activeProject:boss3conf.xml");
    exception_lexicon.setFileName("activeProject:exception_lexicon.txt");
    //end TODO
    if (exception_lexicon.exists())
            ui->checkBox_exception_lexicon->setChecked(true);
    else
        ui->checkBox_exception_lexicon->setChecked(false);

    if (boss3conf.exists())
            ui->checkBox_boss3conf->setChecked(true);
    else
        ui->checkBox_boss3conf->setChecked(false);
    if (filelist.exists())
            ui->checkBox_Filelist->setChecked(true);
    else
        ui->checkBox_Filelist->setChecked(false);

    if (unitlist.exists())
            ui->checkBox_Unitlist->setChecked(true);
    else
        ui->checkBox_Unitlist->setChecked(false);

    if (transcriptionfile.exists())
            ui->checkBox_Transcription->setChecked(true);
    else
        ui->checkBox_Transcription->setChecked(false);

    if (dict.exists())
            ui->checkBox_Dict->setChecked(true);
    else
        ui->checkBox_Dict->setChecked(false);

    if (convert_tables.exists())
            ui->checkBox_Convert->setChecked(true);
    else
        ui->checkBox_Convert->setChecked(false);

}

void BofuGui::on_pushButton_refreshfolder_clicked()
{
    check_existing_files();
}

void BofuGui::writeToLog(QString addLog){
    commandexec = system(addLog.toAscii().data());
    QFile log(""+dir.absolutePath()+"/"+korpusname+".log");
    if(!log.open(QFile::ReadOnly | QFile::Text))
        return;
    QTextStream in(&log);
    ui->logText->setPlainText(in.readAll());
    log.close();
    QTextCursor c = ui->logText->textCursor();
    c.movePosition(QTextCursor::End);
    ui->logText->setTextCursor(c);
}
